{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.5.1 有哪些io方式\n",
    "\n",
    "- 数据分析阶段的重点：分析、建模\n",
    "\n",
    "## 3.5.2 读取和存储csv\n",
    "\n",
    "- 存储、读取、索引设置\n",
    "- 数据追加\n",
    "\n",
    "## 3.5.3 读取和存储excel\n",
    "\n",
    "- 存储、读取、工作表设置\n",
    "- 数据追加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.DataFrame(np.random.randn(1000,3),columns=['a','b','c'],\n",
    "                   index=pd.date_range('20200101',periods=1000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>0.538261</td>\n",
       "      <td>-0.389685</td>\n",
       "      <td>-0.952813</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>-0.622439</td>\n",
       "      <td>-0.090098</td>\n",
       "      <td>-0.120977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>-2.380800</td>\n",
       "      <td>0.754226</td>\n",
       "      <td>0.494995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04</th>\n",
       "      <td>-0.435145</td>\n",
       "      <td>1.709480</td>\n",
       "      <td>-1.018768</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05</th>\n",
       "      <td>0.625383</td>\n",
       "      <td>1.024123</td>\n",
       "      <td>1.755665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-22</th>\n",
       "      <td>-0.465101</td>\n",
       "      <td>1.651035</td>\n",
       "      <td>-0.180905</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-23</th>\n",
       "      <td>0.041655</td>\n",
       "      <td>-0.170946</td>\n",
       "      <td>-0.881767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-24</th>\n",
       "      <td>0.401195</td>\n",
       "      <td>1.003582</td>\n",
       "      <td>0.367173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-25</th>\n",
       "      <td>0.758136</td>\n",
       "      <td>1.825101</td>\n",
       "      <td>-0.410025</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-26</th>\n",
       "      <td>0.010112</td>\n",
       "      <td>-0.410298</td>\n",
       "      <td>-0.141626</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   a         b         c\n",
       "2020-01-01  0.538261 -0.389685 -0.952813\n",
       "2020-01-02 -0.622439 -0.090098 -0.120977\n",
       "2020-01-03 -2.380800  0.754226  0.494995\n",
       "2020-01-04 -0.435145  1.709480 -1.018768\n",
       "2020-01-05  0.625383  1.024123  1.755665\n",
       "...              ...       ...       ...\n",
       "2022-09-22 -0.465101  1.651035 -0.180905\n",
       "2022-09-23  0.041655 -0.170946 -0.881767\n",
       "2022-09-24  0.401195  1.003582  0.367173\n",
       "2022-09-25  0.758136  1.825101 -0.410025\n",
       "2022-09-26  0.010112 -0.410298 -0.141626\n",
       "\n",
       "[1000 rows x 3 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据存储\n",
    "data.to_csv('txt.csv') # route/file.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>0.538261</td>\n",
       "      <td>-0.389685</td>\n",
       "      <td>-0.952813</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-01-02</td>\n",
       "      <td>-0.622439</td>\n",
       "      <td>-0.090098</td>\n",
       "      <td>-0.120977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-01-03</td>\n",
       "      <td>-2.380800</td>\n",
       "      <td>0.754226</td>\n",
       "      <td>0.494995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-01-04</td>\n",
       "      <td>-0.435145</td>\n",
       "      <td>1.709480</td>\n",
       "      <td>-1.018768</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-01-05</td>\n",
       "      <td>0.625383</td>\n",
       "      <td>1.024123</td>\n",
       "      <td>1.755665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>2022-09-22</td>\n",
       "      <td>-0.465101</td>\n",
       "      <td>1.651035</td>\n",
       "      <td>-0.180905</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>2022-09-23</td>\n",
       "      <td>0.041655</td>\n",
       "      <td>-0.170946</td>\n",
       "      <td>-0.881767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>2022-09-24</td>\n",
       "      <td>0.401195</td>\n",
       "      <td>1.003582</td>\n",
       "      <td>0.367173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>2022-09-25</td>\n",
       "      <td>0.758136</td>\n",
       "      <td>1.825101</td>\n",
       "      <td>-0.410025</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>2022-09-26</td>\n",
       "      <td>0.010112</td>\n",
       "      <td>-0.410298</td>\n",
       "      <td>-0.141626</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Unnamed: 0         a         b         c\n",
       "0    2020-01-01  0.538261 -0.389685 -0.952813\n",
       "1    2020-01-02 -0.622439 -0.090098 -0.120977\n",
       "2    2020-01-03 -2.380800  0.754226  0.494995\n",
       "3    2020-01-04 -0.435145  1.709480 -1.018768\n",
       "4    2020-01-05  0.625383  1.024123  1.755665\n",
       "..          ...       ...       ...       ...\n",
       "995  2022-09-22 -0.465101  1.651035 -0.180905\n",
       "996  2022-09-23  0.041655 -0.170946 -0.881767\n",
       "997  2022-09-24  0.401195  1.003582  0.367173\n",
       "998  2022-09-25  0.758136  1.825101 -0.410025\n",
       "999  2022-09-26  0.010112 -0.410298 -0.141626\n",
       "\n",
       "[1000 rows x 4 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = 'txt.csv'\n",
    "pd.read_csv(filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>0.538261</td>\n",
       "      <td>-0.389685</td>\n",
       "      <td>-0.952813</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>-0.622439</td>\n",
       "      <td>-0.090098</td>\n",
       "      <td>-0.120977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>-2.380800</td>\n",
       "      <td>0.754226</td>\n",
       "      <td>0.494995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04</th>\n",
       "      <td>-0.435145</td>\n",
       "      <td>1.709480</td>\n",
       "      <td>-1.018768</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05</th>\n",
       "      <td>0.625383</td>\n",
       "      <td>1.024123</td>\n",
       "      <td>1.755665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-22</th>\n",
       "      <td>-0.465101</td>\n",
       "      <td>1.651035</td>\n",
       "      <td>-0.180905</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-23</th>\n",
       "      <td>0.041655</td>\n",
       "      <td>-0.170946</td>\n",
       "      <td>-0.881767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-24</th>\n",
       "      <td>0.401195</td>\n",
       "      <td>1.003582</td>\n",
       "      <td>0.367173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-25</th>\n",
       "      <td>0.758136</td>\n",
       "      <td>1.825101</td>\n",
       "      <td>-0.410025</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-26</th>\n",
       "      <td>0.010112</td>\n",
       "      <td>-0.410298</td>\n",
       "      <td>-0.141626</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   a         b         c\n",
       "2020-01-01  0.538261 -0.389685 -0.952813\n",
       "2020-01-02 -0.622439 -0.090098 -0.120977\n",
       "2020-01-03 -2.380800  0.754226  0.494995\n",
       "2020-01-04 -0.435145  1.709480 -1.018768\n",
       "2020-01-05  0.625383  1.024123  1.755665\n",
       "...              ...       ...       ...\n",
       "2022-09-22 -0.465101  1.651035 -0.180905\n",
       "2022-09-23  0.041655 -0.170946 -0.881767\n",
       "2022-09-24  0.401195  1.003582  0.367173\n",
       "2022-09-25  0.758136  1.825101 -0.410025\n",
       "2022-09-26  0.010112 -0.410298 -0.141626\n",
       "\n",
       "[1000 rows x 3 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(filename,index_col=['Unnamed: 0'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04',\n",
       "               '2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08',\n",
       "               '2020-01-09', '2020-01-10',\n",
       "               ...\n",
       "               '2022-09-17', '2022-09-18', '2022-09-19', '2022-09-20',\n",
       "               '2022-09-21', '2022-09-22', '2022-09-23', '2022-09-24',\n",
       "               '2022-09-25', '2022-09-26'],\n",
       "              dtype='datetime64[ns]', length=1000, freq='D')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 存储前对数据索引进行命名：date\n",
    "data.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "FrozenList([None])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.index.names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.index.names = ['date']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>0.538261</td>\n",
       "      <td>-0.389685</td>\n",
       "      <td>-0.952813</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>-0.622439</td>\n",
       "      <td>-0.090098</td>\n",
       "      <td>-0.120977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>-2.380800</td>\n",
       "      <td>0.754226</td>\n",
       "      <td>0.494995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04</th>\n",
       "      <td>-0.435145</td>\n",
       "      <td>1.709480</td>\n",
       "      <td>-1.018768</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-05</th>\n",
       "      <td>0.625383</td>\n",
       "      <td>1.024123</td>\n",
       "      <td>1.755665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-22</th>\n",
       "      <td>-0.465101</td>\n",
       "      <td>1.651035</td>\n",
       "      <td>-0.180905</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-23</th>\n",
       "      <td>0.041655</td>\n",
       "      <td>-0.170946</td>\n",
       "      <td>-0.881767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-24</th>\n",
       "      <td>0.401195</td>\n",
       "      <td>1.003582</td>\n",
       "      <td>0.367173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-25</th>\n",
       "      <td>0.758136</td>\n",
       "      <td>1.825101</td>\n",
       "      <td>-0.410025</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-26</th>\n",
       "      <td>0.010112</td>\n",
       "      <td>-0.410298</td>\n",
       "      <td>-0.141626</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   a         b         c\n",
       "date                                    \n",
       "2020-01-01  0.538261 -0.389685 -0.952813\n",
       "2020-01-02 -0.622439 -0.090098 -0.120977\n",
       "2020-01-03 -2.380800  0.754226  0.494995\n",
       "2020-01-04 -0.435145  1.709480 -1.018768\n",
       "2020-01-05  0.625383  1.024123  1.755665\n",
       "...              ...       ...       ...\n",
       "2022-09-22 -0.465101  1.651035 -0.180905\n",
       "2022-09-23  0.041655 -0.170946 -0.881767\n",
       "2022-09-24  0.401195  1.003582  0.367173\n",
       "2022-09-25  0.758136  1.825101 -0.410025\n",
       "2022-09-26  0.010112 -0.410298 -0.141626\n",
       "\n",
       "[1000 rows x 3 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.to_csv(filename) # 完全覆盖/替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对已有文件进行数据追加\n",
    "data2 = data.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2022-09-22</th>\n",
       "      <td>-0.465101</td>\n",
       "      <td>1.651035</td>\n",
       "      <td>-0.180905</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-23</th>\n",
       "      <td>0.041655</td>\n",
       "      <td>-0.170946</td>\n",
       "      <td>-0.881767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-24</th>\n",
       "      <td>0.401195</td>\n",
       "      <td>1.003582</td>\n",
       "      <td>0.367173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-25</th>\n",
       "      <td>0.758136</td>\n",
       "      <td>1.825101</td>\n",
       "      <td>-0.410025</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-09-26</th>\n",
       "      <td>0.010112</td>\n",
       "      <td>-0.410298</td>\n",
       "      <td>-0.141626</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   a         b         c\n",
       "date                                    \n",
       "2022-09-22 -0.465101  1.651035 -0.180905\n",
       "2022-09-23  0.041655 -0.170946 -0.881767\n",
       "2022-09-24  0.401195  1.003582  0.367173\n",
       "2022-09-25  0.758136  1.825101 -0.410025\n",
       "2022-09-26  0.010112 -0.410298 -0.141626"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 首先判断是否有数据，有的情况下可以这么操作：\n",
    "data2.to_csv(filename,mode='a',header=False) # append - 追加操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# excel\n",
    "excelname = 'excel.xlsx'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.to_excel(excelname,sheet_name='a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-01-01</td>\n",
       "      <td>0.538261</td>\n",
       "      <td>-0.389685</td>\n",
       "      <td>-0.952813</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-01-02</td>\n",
       "      <td>-0.622439</td>\n",
       "      <td>-0.090098</td>\n",
       "      <td>-0.120977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-01-03</td>\n",
       "      <td>-2.380800</td>\n",
       "      <td>0.754226</td>\n",
       "      <td>0.494995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-01-04</td>\n",
       "      <td>-0.435145</td>\n",
       "      <td>1.709480</td>\n",
       "      <td>-1.018768</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-01-05</td>\n",
       "      <td>0.625383</td>\n",
       "      <td>1.024123</td>\n",
       "      <td>1.755665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>2022-09-22</td>\n",
       "      <td>-0.465101</td>\n",
       "      <td>1.651035</td>\n",
       "      <td>-0.180905</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>2022-09-23</td>\n",
       "      <td>0.041655</td>\n",
       "      <td>-0.170946</td>\n",
       "      <td>-0.881767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>2022-09-24</td>\n",
       "      <td>0.401195</td>\n",
       "      <td>1.003582</td>\n",
       "      <td>0.367173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>2022-09-25</td>\n",
       "      <td>0.758136</td>\n",
       "      <td>1.825101</td>\n",
       "      <td>-0.410025</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>2022-09-26</td>\n",
       "      <td>0.010112</td>\n",
       "      <td>-0.410298</td>\n",
       "      <td>-0.141626</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          date         a         b         c\n",
       "0   2020-01-01  0.538261 -0.389685 -0.952813\n",
       "1   2020-01-02 -0.622439 -0.090098 -0.120977\n",
       "2   2020-01-03 -2.380800  0.754226  0.494995\n",
       "3   2020-01-04 -0.435145  1.709480 -1.018768\n",
       "4   2020-01-05  0.625383  1.024123  1.755665\n",
       "..         ...       ...       ...       ...\n",
       "995 2022-09-22 -0.465101  1.651035 -0.180905\n",
       "996 2022-09-23  0.041655 -0.170946 -0.881767\n",
       "997 2022-09-24  0.401195  1.003582  0.367173\n",
       "998 2022-09-25  0.758136  1.825101 -0.410025\n",
       "999 2022-09-26  0.010112 -0.410298 -0.141626\n",
       "\n",
       "[1000 rows x 4 columns]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_excel(excelname)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.to_excel(excelname,sheet_name='b') # 没有mode参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.to_excel?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一次写入多个sheet\n",
    "with pd.ExcelWriter('writer.xlsx') as writer:\n",
    "    data.to_excel(writer,sheet_name='a')\n",
    "    data.to_excel(writer,sheet_name='b')\n",
    "    data.to_excel(writer,sheet_name='c')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 追加新sheet\n",
    "with pd.ExcelWriter('writer.xlsx',mode='a',engine='openpyxl') as writer:\n",
    "    data2.to_excel(writer,sheet_name='d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试excel追加数据至sheet\n",
    "with pd.ExcelWriter('writer.xlsx',mode='a',engine='openpyxl') as writer:\n",
    "    data.to_excel(writer,sheet_name='d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# openpyxl库：读取 -> 追加新数据 -> 存入表格中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
